import os
from flask import current_app

from app import create_app, db
from app.models import User
from celery_task import app


@app.task
def update_token(json_post, user_id):
    app = create_app(os.environ.get('ENV_TYPE', "development"))
    with app.app_context():
        user = User.query.get(user_id)
        user_info = current_app.auth_token.verifyLogin(username=json_post.get('account'),
                                                       password=json_post.get('password'))
        if user_info.get('status') == 1:
            user.token = user_info.get('token')
            db.session.add(user)
            db.session.commit()
        return 'ok'


